//
//  PlaceholderTextView.m
//  Position
//
//  Created by 汪的一声就哭了！ on 16/5/25.
//  Copyright © 2016年 汪的一声就哭了！. All rights reserved.
//

#import "PlaceholderTextView.h"

@interface PlaceholderTextView ()
@property (nonatomic, strong) UILabel *placeholderLabel;
@end
@implementation PlaceholderTextView
-(UILabel *)placeholderLabel
{
    if (!_placeholderLabel) {
        _placeholderLabel = [[UILabel alloc]initWithFrame:CGRectMake(4, 8, self.bounds.size.width-8, 0)];
        [_placeholderLabel sizeToFit];
        _placeholderLabel.font = [UIFont systemFontOfSize:14];
        _placeholderLabel.textColor = [UIColor grayColor];
        _placeholderLabel.numberOfLines = 0;
        [self addSubview:_placeholderLabel];
    }
    return _placeholderLabel;
}
/**
 *  设置自己的属性
 */
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.alwaysBounceVertical = YES;
        self.textColor = [UIColor blackColor];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(texting) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}
/**
 *  监听有文字输入
 */
-(void)texting
{
    [self setPlaceholderTextShow];
}
/**
 *  设置占位文字的显示
 */
-(void)setPlaceholderTextShow
{
    self.placeholderLabel.hidden = self.hasText;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    self.placeholderLabel.frame = CGRectMake(4, 8, self.bounds.size.width-8, 0);
    [self.placeholderLabel sizeToFit];//这一步很重要，不能遗忘
}
-(void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;//此句的意义何在？
    self.placeholderLabel.text = placeholder;
    [self setNeedsLayout];
}
-(void)setPlaceholderColor:(UIColor *)placeholderColor
{
    _placeholderColor = placeholderColor;
    self.placeholderLabel.textColor = placeholderColor;
    [self setNeedsLayout];
}
-(void)setFont:(UIFont *)font
{
    [super setFont:font];
    self.placeholderLabel.font = font;
    [self setNeedsLayout];
}
-(void)setText:(NSString *)text
{
    [super setText:text];
    [self setPlaceholderTextShow];
}
-(void)setAttributedText:(NSAttributedString *)attributedText
{
    [super setAttributedText:attributedText];
    [self setPlaceholderTextShow];
}
@end
